package swing.admin.views.aside;

import swing.admin.common.AdminContextHolder;
import swing.admin.common.Constant;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class AsideMenuItem extends JPanel {
    public String name;
    public Boolean selected = false;

    public AsideMenuItem(String name, List<AsideMenuItem> list) {
        this.name = name;
        this.setBackground(new Color(250, 250, 250));
        this.setPreferredSize(new Dimension(Constant.Aside.DEFAULT_MENU_WIDTH, Constant.Aside.DEFAULT_MENU_HEIGHT));

        JLabel label = new JLabel(this.name);
        label.setFont(AdminContextHolder.getContext().getDefaultFont());
        label.setBackground(Color.ORANGE);

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.CENTER;
        this.setLayout(new GridBagLayout());
        this.add(label, c);

        refreshBackground(list);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                mouseEnteredRefresh(list);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                mouseExitedRefresh(list);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                refreshBackground(list);
            }
        });
    }

    private void mouseExitedRefresh(List<AsideMenuItem> chatterList) {
        for (AsideMenuItem item : chatterList) {
            if (!item.selected) {
                item.setBackground(new Color(250, 250, 250));
            }
        }
    }

    private void mouseEnteredRefresh(List<AsideMenuItem> chatterList) {
        for (AsideMenuItem item : chatterList) {
            if (item.equals(this) && !item.selected) {
                item.setBackground(new Color(245, 245, 245));
            }
        }
    }

    private void refreshBackground(List<AsideMenuItem> chatterList) {
        for (AsideMenuItem item : chatterList) {
            item.selected = item.equals(this);
            item.setBackground(item.selected ? new Color(0, 153, 255) : new Color(250, 250, 250));
        }
    }
}
